package com.ivvi.autoreco.source.local;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import com.ivvi.autoreco.model.LocalImage;
import com.ivvi.autoreco.source.ImageDataSource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by duanxianping on 2017/3/8.
 * <p>
 * query local 2d 3d image
 */

public class ImageLocalDataSource implements ImageDataSource {

    private final static String TAG = ImageLocalDataSource.class.getSimpleName();

    /**
     * 查询图片
     */
    private HashMap<String, List<LocalImage>> queryAllExternalGroup(ContentResolver contentResolver) {
        HashMap<String, List<LocalImage>> hashMap = new HashMap<String, List<LocalImage>>();
        Cursor cursor = null;
        try {
            cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
            if (null != cursor && cursor.moveToFirst()) {
                final int index__ID = cursor.getColumnIndex(MediaStore.Images.Media._ID);
                final int index_DATA = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                final int index_SIZE = cursor.getColumnIndex(MediaStore.Images.Media.SIZE);
                final int index_DATE_MODIFIED = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED);
                // 分组信息
                final int index_BUCKET_ID = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
                final int index_BUCKET_DISPLAY_NAME = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
                do {
                    // String BUCKET_ID =
                    // cursor.getColumnName(index_BUCKET_ID);
                    String bucket_display_name = cursor.getString(index_BUCKET_DISPLAY_NAME);

                    LocalImage model = new LocalImage();
                    model.set_id(cursor.getInt(index__ID));
                    model.setAbsPath(cursor.getString(index_DATA));
                    model.setSize(cursor.getLong(index_SIZE));
                    model.setLastModified(cursor.getLong(index_DATE_MODIFIED));
                    model.setGroup(cursor.getString(index_BUCKET_DISPLAY_NAME));
                    model.setBucketId(cursor.getLong(index_BUCKET_ID));

                    List<LocalImage> list = hashMap.get(bucket_display_name);
                    if (null == list) {
                        list = new ArrayList<LocalImage>();
                        hashMap.put(bucket_display_name, list);
                    }
                    list.add(model);
                } while (cursor.moveToNext());
            }
        } catch (Exception ex) {
            Log.e(TAG, ex.getMessage());
            ex.printStackTrace();
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
        return hashMap;
    }

    /**
     * 查询图片
     */
    private HashMap<String, List<LocalImage>> query(ContentResolver contentResolver, String whereLike, String orderBy) {
        HashMap<String, List<LocalImage>> hashMap = new HashMap<String, List<LocalImage>>();
        Cursor cursor = null;
        try {
            cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, whereLike, null, orderBy);
            if (null != cursor && cursor.moveToFirst()) {
                final int index__ID = cursor.getColumnIndex(MediaStore.Images.Media._ID);
                final int index_DATA = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                final int index_SIZE = cursor.getColumnIndex(MediaStore.Images.Media.SIZE);
                final int index_DATE_MODIFIED = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED);
                // 分组信息
                final int index_BUCKET_ID = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
                final int index_BUCKET_DISPLAY_NAME = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
                do {
                    // String BUCKET_ID =
                    // cursor.getColumnName(index_BUCKET_ID);
                    String bucket_display_name = cursor.getString(index_BUCKET_DISPLAY_NAME);

                    LocalImage model = new LocalImage();
                    model.set_id(cursor.getInt(index__ID));
                    model.setAbsPath(cursor.getString(index_DATA));
                    model.setSize(cursor.getLong(index_SIZE));
                    model.setLastModified(cursor.getLong(index_DATE_MODIFIED));
                    model.setGroup(cursor.getString(index_BUCKET_DISPLAY_NAME));
                    model.setBucketId(cursor.getLong(index_BUCKET_ID));

                    List<LocalImage> list = hashMap.get(bucket_display_name);
                    if (null == list) {
                        list = new ArrayList<LocalImage>();
                        hashMap.put(bucket_display_name, list);
                    }
                    list.add(model);
                } while (cursor.moveToNext());
            }
        } catch (Exception ex) {
            Log.e(TAG, ex.getMessage());
            ex.printStackTrace();
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
        return hashMap;
    }

    private LocalImage findbyUri(ContentResolver contentResolver, Uri uri) {
        LocalImage model = null;
        Cursor cursor = contentResolver.query(uri, null, null, null, null);
        if (null != cursor && cursor.moveToFirst()) {
            final int index__ID = cursor.getColumnIndex(MediaStore.Images.Media._ID);
            final int index_DATA = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
            final int index_SIZE = cursor.getColumnIndex(MediaStore.Images.Media.SIZE);
            final int index_DATE_MODIFIED = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED);
            final int index_BUCKET_ID = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
            final int index_BUCKET_DISPLAY_NAME = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);

            model = new LocalImage();
            model.set_id(cursor.getInt(index__ID));
            model.setAbsPath(cursor.getString(index_DATA));
            model.setSize(cursor.getLong(index_SIZE));
            model.setLastModified(cursor.getLong(index_DATE_MODIFIED));
            model.setGroup(cursor.getString(index_BUCKET_DISPLAY_NAME));
            model.setBucketId(cursor.getLong(index_BUCKET_ID));
        }
        return model;
    }

    /**
     * 查询图片
     */
    private List<LocalImage> queryForList(ContentResolver contentResolver, String whereLike, String orderBy) {
        List<LocalImage> list = new ArrayList<>();
        Cursor cursor = null;
        try {
            cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, whereLike, null, orderBy);
            if (null != cursor && cursor.moveToFirst()) {
                final int index__ID = cursor.getColumnIndex(MediaStore.Images.Media._ID);
                final int index_DATA = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                final int index_SIZE = cursor.getColumnIndex(MediaStore.Images.Media.SIZE);
                final int index_DATE_MODIFIED = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED);
                // 分组信息
                final int index_BUCKET_ID = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
                final int index_BUCKET_DISPLAY_NAME = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
                do {
                    // String BUCKET_ID =cursor.getColumnName(index_BUCKET_ID);
                    // String bucket_display_name = cursor.getString(index_BUCKET_DISPLAY_NAME);

                    LocalImage model = new LocalImage();
                    model.set_id(cursor.getInt(index__ID));
                    model.setAbsPath(cursor.getString(index_DATA));
                    model.setSize(cursor.getLong(index_SIZE));
                    model.setLastModified(cursor.getLong(index_DATE_MODIFIED));
                    model.setGroup(cursor.getString(index_BUCKET_DISPLAY_NAME));
                    model.setBucketId(cursor.getLong(index_BUCKET_ID));

                    list.add(model);
                } while (cursor.moveToNext());
            }
        } catch (Exception ex) {
            Log.e(TAG, ex.getMessage());
            ex.printStackTrace();
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
        return list;
    }

    @Override
    public void loadLocalImages(final Context context, final LoadImagesCallback callback) {

        final ContentResolver contentResolver = context.getContentResolver();
        new Thread(new Runnable() {
            @Override
            public void run() {
                HashMap<String, List<LocalImage>> hashMap = queryAllExternalGroup(contentResolver);
                callback.loadImages(hashMap);
            }
        }).start();
    }


    @Override
    public void loadByUri(final Context context, final Uri uri, final LoadSimgleImageCallback callback) {
        final ContentResolver contentResolver = context.getContentResolver();
        new Thread(new Runnable() {
            @Override
            public void run() {
                LocalImage image = findbyUri(contentResolver, uri);
                callback.loadSingleImage(image);
            }
        }).start();
    }
}
